import java.nio.charset.Charset;
import java.sql.SQLOutput;
import java.util.Arrays;

public class javaBasic2 {
    public static void main(String[] args){
        String origin = "hello world 中国";
        char v1 = origin.charAt(13); // 取出字符
        System.out.println(v1);

        int len = origin.length(); // 字符串长度
        for ( int i = 0 ; i < len ; i++){
            char item = origin.charAt(i);
            System.out.println(item);
        }

        String v2 = origin.trim(); // 去除空白
        System.out.println(v2);

        String v3 = origin.toLowerCase(); // 小写
        System.out.println(v3);

        String v4 = origin.toUpperCase();
        System.out.println(v4);


        String[] v5 = origin.split(" ");
        System.out.println(v5);
        System.out.println(Arrays.toString(v5));

        String v6 = origin.replace("h", "l"); // 把h替换为l
        System.out.println(v6);
        String v7 = origin.substring(2, 6);  // 子字符串=切片 [2:6]
        System.out.println(v7);
        boolean v8 = origin.equals("hello world"); // 比较字符串是否相等
        System.out.println(v8);
        boolean v9 = origin.contains("中"); // 字符串是否包含
        System.out.println(v9);
        boolean v10 = origin.startsWith("a"); // 字符串是否以xx开头
        System.out.println(v10);
        String v11 = origin.concat("哈哈哈"); //在字符串末尾追加
        System.out.println(v11);


        //      ### 1 直接相加，效率低
        String s="hello"+"world"+"中国";

// 2 使用StringBuilder或StringBuffer拼接
//        StringBuilder sb = new StringBuilder();  // StringBuffer线程安全
        StringBuffer sb = new StringBuffer();  // StringBuffer线程安全
        sb.append("hello");
        sb.append(" ");
        sb.append("world");
        sb.append(" ");
        sb.append("中");
        sb.append("国");
        System.out.println(sb.toString());


        // 2 字节数组转字符串
        String v81 = new String(new byte[]{-27, -67, -83, -28, -70, -114, -26, -103, -113});
        String v91 = new String(new byte[]{-59, -19, -45, -38, -22, -52},Charset.forName("GBK"));

        System.out.println(v81);
        System.out.println(v91);

        // 1 字符数组转字符串
        char[] c=new char[]{'彭','于','晏'};
        String v121 = new String(c);
        System.out.println(v121);

        // 2 字符串串转字符数组
        String str1 = "彭于晏";
        char[] c1 =str1.toCharArray();
        System.out.println(c1.length);
        System.out.println(c1[0]);


    }
}
